<?php
/**
 * Created by PhpStorm.
 * User: yhf
 * Date: 2019-04-19
 * Time: 15:06
 */

namespace app\api\service;

use app\api\model\User as UserModel;
use app\lib\exception\UserException;
use app\api\model\Tp as TpModel;

class AddUser extends User
{
    // 添加学生
    public static function UserAdd ($name,$pwd) {
        if (!UserModel::getByUser($name)) {
            $pwds = self::generateToken($pwd);
            UserModel::create([
                'name' => $name,
                'pwd' => $pwds
            ]);
            return true;
        }
        throw new UserException([
            'code' => 203,
            'msg' => '用户以存在请使用账号密码登录',
            'errorCode' => 20000
        ]);
    }

    // 账号登录
    public static function UserLogin ($uid,$name,$pwd) {
        $pwds = self::generateToken($pwd);

        $Login = UserModel::where('id','=',$uid)->where('name','=',$name)
            ->where('pwd', '=',$pwds)
            ->find();
        if ($Login) {
            return true;
        }
        throw new UserException([
	    'code' => 203,
            'msg' => '用户名或密码错误',
            'errorCode' => 20002
        ]);
    }


    public static function getTpAdd ($uid,$date) {

        $user_data = UserModel::getID($uid);
        $tp_data = TpModel::getByTp();
        if ($user_data->tp != null) {
            return false;
        } else {
            switch($date)
            {
                case '18会展班':
                    $date2 = 't1';
                    $tp_data->tp1 = $tp_data->tp1 +1;
                    break;
                case '18工艺班':
                    $date2 = 't2';
                    $tp_data->tp2 = $tp_data->tp2 +1;
                    break;
                case '18计美1班':
                    $date2 = 't3';
                    $tp_data->tp3 = $tp_data->tp3 +1;
                    break;
                case '18计美2班':
                    $date2 = 't4';
                    $tp_data->tp4 = $tp_data->tp4 +1;
                    break;
                case '18配饰班':
                    $date2 = 't5';
                    $tp_data->tp5 = $tp_data->tp5 +1;
                    break;
                case '18陶艺班':
                    $date2 = 't6';
                    $tp_data->tp6 = $tp_data->tp6 +1;
                    break;
            }

            $user_data->tp = $date2;
            $user_data->save();
            $tp_data->save();
            return true;
        }
    }
}
